package com.itao.springboot.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.task.TaskSchedulingProperties;
import org.springframework.boot.task.ThreadPoolTaskSchedulerBuilder;
import org.springframework.boot.task.ThreadPoolTaskSchedulerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * @Author sjt
 * @CreateTime 2023/10/13 9:42
 */
@Configuration
public class ScheduleConfig {

    private static final Integer CPU_NUM = Runtime.getRuntime().availableProcessors();
    @Bean("dynamicTaskScheduler")
    public ThreadPoolTaskScheduler dynamicTaskScheduler(
            TaskSchedulingProperties properties,
            ObjectProvider<ThreadPoolTaskSchedulerCustomizer> taskSchedulerCustomizers
    ) {
        ThreadPoolTaskSchedulerBuilder builder = new ThreadPoolTaskSchedulerBuilder();
        builder = builder.poolSize(CPU_NUM);
        TaskSchedulingProperties.Shutdown shutdown = properties.getShutdown();
        builder = builder.awaitTermination(shutdown.isAwaitTermination());
        builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
        builder = builder.threadNamePrefix("dynamicTaskScheduler-");
        builder = builder.customizers(taskSchedulerCustomizers);
        return builder.build();
    }


    /**
     * 单机模式
     */
    @Bean
    public static RedissonClient redissonSingle(){
        Config config = new Config();
        // linux中的配置
        // config.setTransportMode(TransportMode.EPOLL);
        config.useSingleServer()
                .setAddress("redis://39.105.80.131:3978")
                .setPassword("haoyue@123456..")
                .setDatabase(1);
        return Redisson.create(config);
    }
}
